from bs4 import BeautifulSoup
import requests
import sys
from tqdm import tqdm


class Downloader():
    
    def __init__(self) -> None:
        self.server = 'https://www.biqukan8.cc/'
        self.target_url = 'https://www.biqukan8.cc/1_1094/'
        self.headers = {
            'authority': 'developer.mozilla.org',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'max-age=0',
            'if-modified-since': 'Mon, 01 Jan 2024 01:39:55 GMT',
            'if-none-match': 'W/"918634a4ef0ed92c43fa7a08593e3df5"',
            'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'sec-fetch-dest': 'document',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-user': '?1',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
            }
        self.names = [] #章节名
        self.urls = [] #章节链接
        self.nus = 0 #章节数



    '''
    获取章节链接
    '''
    def get_donload_url(self):
        req = requests.get(url=self.target_url, headers=self.headers)
        div = (BeautifulSoup(req.text, features='lxml')).find_all('div', class_='listmain')
        a = (BeautifulSoup(str(div[0]), features='lxml')).find_all('a')
        self.nums = len(a[13:])
        for _ in a:
            self.names.append(_.string)
            self.urls.append(self.server + _.get('href', ''))

    '''
    获取章节内容
    '''
    def get_content(self, target):
        req = requests.get(url=target,headers=self.headers)
        texts = (BeautifulSoup(req.text, features='lxml')).find_all('div', class_='showtxt')
        texts = texts[0].text.replace('\xa0'*8, '\n\n')
        return texts

        

    '''
    将章节内容写入文件
    '''
    def writer(self, name, path, text):
        write_flag = True
        with open(path, 'a', encoding='utf-8') as f:
            f.write(name + '\n')
            f.writelines(text)
            f.write('\n\n')


if __name__ == '__main__':
    dl = Downloader()
    dl.get_donload_url()
    print('开始下载......')
    for i in tqdm(range(dl.nums)):
        dl.writer(dl.names[i], '一念永恒.txt', dl.get_content(dl.urls[i]))
        sys.stdout.write("  已下载:%.3f%%" %  float(i/dl.nums) + '\r')
        sys.stdout.flush()
    print('下载完成')